package com.thh.shirodemo.controller;

import com.thh.shirodemo.bean.User;
import com.thh.shirodemo.mapper.UserMapper;
import com.thh.shirodemo.util.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 2022-07-20-16:00
 */
@Controller
public class RegisterManager {
    @Resource
    UserMapper userMapper;


    /**
     * 去注册页面
     */
    @RequestMapping("/goRegist")
    public String goRegist(){
        return "regist";
    }
    /**
     * 注册一个admin
     * @param user
     * @return
     */
    @PostMapping("/doRegist")
    public String registerUser(User user,Model model){
        try {
            user.setUserPass(PasswordEncoder.encoder(user.getUserPass()));
            userMapper.insertManage(user);
            model.addAttribute("registMsg","注册成功!");
            return "regist";
        }catch (Exception exception){
            model.addAttribute("registMsg","用户名不能重复哦!");
            return "regist";
        }
    }
}
